package edu.wata.service;

import java.util.List;

import org.apache.log4j.Logger;

import edu.wata.dao.ArticleTypeJdbcDaoImpl;
import edu.wata.dao.iface.ArticleTypeDao;
import edu.wata.domain.ArticleType;
import edu.wata.service.iface.ArticleTypeService;

public class ArticleTypeServiceImpl implements ArticleTypeService {

	private static final Logger log = Logger.getLogger(ArticleTypeServiceImpl.class);

	private ArticleTypeDao articleTypeDao = new ArticleTypeJdbcDaoImpl();

	@Override
	public List<ArticleType> getParentType() {
		return articleTypeDao.getParentType();
	}

	@Override
	public List<ArticleType> getSubType() {
		return articleTypeDao.getSubType();
	}

	@Override
	public List<ArticleType> getAllType() {
		List<ArticleType> parentTypes = articleTypeDao.getParentType();
		List<ArticleType> subTypes = articleTypeDao.getSubType();
		parentTypes.forEach((parentNode) -> {
			setSubNode(parentNode, subTypes);
		});
		return parentTypes;
	}

	private void setSubNode(ArticleType parentNode, List<ArticleType> subTypes) {
		subTypes.stream().filter((subNode) -> subNode.getParentId() == parentNode.getId()).forEach((subNode) -> {
			parentNode.getSubArticleTypes().add(subNode);
		});

	}

}
